using System;
using System.Linq;

namespace Kollektiv.DB
{
	public abstract class DataRepository
	{
		public abstract Type DataType{ get; }
		
		/// <summary>
		/// Gets the related data manager instance.
		/// </summary>
		public DataManager Manager{ get; private set; }
		
		public DataRepository (DataManager manager)
		{
			this.Manager = manager;
		}
	}
	
	/// <summary>
	/// Repository managing a persistent data object.
	/// </summary>
	public class DataRepository<T>:DataRepository where T:DataObject
	{
		/// <summary>
		/// Gets the type of the data object.
		/// </summary>
		public override Type DataType { get { return typeof(T); } }
		
		/// <summary>
		/// Gets a query of all data objects in this repository.
		/// </summary>
		public IQueryable<T> All {
			get {
				return this.Manager.Get<T> ();
			}
		}
		
		/// <summary>
		/// Initializes a new instance of the <see cref="Kollektiv.DB.DataRepository`1"/> class.
		/// </summary>
		/// <param name='manager'>
		/// Related data manager instance.
		/// </param>
		public DataRepository (DataManager manager):base(manager)
		{
		}
		
		/// <summary>
		/// Saves or updates a data object.
		/// </summary>
		/// <param name='obj'>
		/// Affecting data object.
		/// </param>
		public void SaveOrUpdate (T obj)
		{
			this.Manager.SaveOrUpdate (obj);
		}
		
		/// <summary>
		/// Deletes a data object.
		/// </summary>
		/// <param name='obj'>
		/// Affecting data object.
		/// </param>
		public void Delete (T obj)
		{
			this.Manager.Delete (obj);
		}
	}
}

